/*
 **********************************************************
 *
 * Program		:	slist.c
 *
 * written by	:	BAYRAMOV Matin
 *
 * description	:	a library with some functions to use simple
 * 					lists in C
 *
 * date			:	April 21, 2013
 *
 ***********************************************************
 */


#include "slist.h"

/*
 * Function		:	printSlist
 *
 * Authors		:	BAYRAMOV Matin & SHEIKHI Sarah
 *
 * Parameters	:	List list - a list
 *
 * Return		:	RES_OK - it's OK
 *
 * Description 	:	Print the content of a simple list
 *
 * Complexity	:	O(n) - n is the number of elements in the list
 */
int printSlist(List list) {

	printf("\nList content : ");
	while (list != NULL) {
		printf(" %d ", list->value);
		list = list->next;
	}
	printf("\n");

	return RES_OK;
}

/*
 * Function		:	getSizeSlist
 *
 * Authors		:	BAYRAMOV Matin & SHEIKHI Sarah
 *
 * Parameters	:	List list - a list
 *
 * Return		:	int - number of elements in the list
 *
 * Description 	:	Count the number of elements of a list
 *
 * Complexity	:	O(n) - n is the number of elements in the list
 */
int getSizeSlist(List list) {

 	// If there is nothing then size is zero
 	if (list == NULL) {
 		return 0;
 	}

 	// Then let's see what is the size of the rest
 	return 1 + getSizeSlist(list->next);
}

/*
 * Function		:	freeSlist
 *
 * Authors		:	BAYRAMOV Matin & SHEIKHI Sarah
 *
 * Parameters	:	List list - a list
 *
 * Return		:	RES_OK - it's OK
 *
 * Description 	:	Free the memory of a simple list
 *
 * Complexity	:	O(n) - n is the number of elements in the list
 */
int freeSlist(List list) {

	if (list == NULL) {
		return RES_OK;
	}

	List remained = NULL;
	remained = list->next;

	free(list);

	freeSlist(remained);

	return RES_OK;
}

/*
 * Function		:	addBeginSlistElement
 *
 * Authors		:	BAYRAMOV Matin & SHEIKHI Sarah
 *
 * Parameters	:	List list - a list
 *
 * Return		:	List list - new list
 *
 * Description 	:	Add an element to the beginning of a simple list
 *
 * Complexity	:	O(1)
 */
List addBeginSlistElement(List list, int element) {

	sElement * newElement = NULL;
	newElement = malloc (sizeof (sElement) );

	if (newElement == NULL) {
		return NULL;
	}

	newElement->value = element;
	newElement->next = list;

	return newElement;
}


/*
 * Function		:	addEndSlistElement
 *
 * Authors		:	BAYRAMOV Matin & SHEIKHI Sarah
 *
 * Parameters	:	List list - a list
 *
 * Return		:	List list - a new list with added element
 *
 * Description 	:	Add an element to the end of a simple list
 *
 * Complexity	:	O(n) - n is the number of elements in the list
 */
List addEndSlistElement(List list, int element) {

	sElement * newElement = NULL;
	newElement = malloc (sizeof (sElement) );

	if (newElement == NULL) {
		return NULL;
	}

	newElement->value = element;
	newElement->next = NULL;

	if (list == NULL) {
		return newElement;
	}

	List temp = list;
	while (temp->next != NULL) {
		temp = temp->next;
	}
	temp->next = newElement;

	return list;
}


/*
 * Function		:	removeSlistElement
 *
 * Authors		:	BAYRAMOV Matin & SHEIKHI Sarah
 *
 * Parameters	:	List list - a list
 * 					List *element - an element to remove
 *
 * Return		:	RES_OK - it's ok
 *
 * Description 	:	Remove an element from a simple list
 *
 * Complexity	:	O(n) - n is the number of elements in the list
 */
int removeSlistElement(List list, int element) {

	// Must be implemented in case of need

	if (list != NULL) { freeSlist(list); }

	if (element <= 0) {

	}

	return RES_OK;
}


/*
 * Function		:	addEndSlistElement
 *
 * Authors		:	BAYRAMOV Matin & SHEIKHI Sarah
 *
 * Parameters	:	List list - a list
 * 					List *clone - to save the cloned list
 *
 * Return		:	RES_OK - it's ok
 *
 * Description 	:	Clone a simple list
 *
 * Complexity	:	O(n) - n is the number of elements in the list
 */
int cloneSlist(List list, List * clone) {

	while (list != NULL) {
		(*clone) = addEndSlistElement((*clone), list->value);
		list = list->next;
	}

	return RES_OK;
}


